import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class kdf4 {
	public int xRow; int xCol;
	int iWait;
	
	@Test
	public void qS1() throws Exception{
		iWait = 3000;
		//Thread.sleep(iWait);
		String[][] xTC, xTS;
		String vKW, vXpath, vTestData;
		FirefoxDriver myD = new FirefoxDriver();
	
		
		String xPath = "C:/Users/priyap@airloom.com.au/Desktop.xls";
		
		//Read the entire TC and TS
		xTC = xlRead(xPath,"TestCases");
		xTS = xlRead(xPath, "TestSteps");
		
		
		System.out.println("Test Step sheet is" + xTS);
		
		//Go to each row in TC and identify which TC to execute
		
		for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC
			System.out.println("TCID  is " + xTC[i][0]);
			//Thread.sleep(iWait);
			if (xTC[i][3].equalsIgnoreCase("Y")) {
				Thread.sleep(iWait);
				System.out.println("Yes, execute this");
				Thread.sleep(iWait);
			
			//go to every test step if it is y
			for (int j=1; j<xTS.length; j++){
				//Thread.sleep(iWait);
				System.out.println("TSID is" +xTS[i][0]);
				Thread.sleep(iWait);
				if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
					//Thread.sleep(iWait);
					System.out.println("KW to execute is " + xTS[j][4]);
					Thread.sleep(iWait);
					System.out.println("xpath to execute is " + xTS[j][5]);
					System.out.println("Data to execute is " + xTS[j][6]);
					
					
					// call the corresponding KW function
					vKW = xTS[j][4] ;
					vXpath = xTS[j][5];
					vTestData = xTS[j][6];
					
					if (vKW.equalsIgnoreCase("enterText")) {
						enterText(myD, vXpath, vTestData);
					}
						if (vKW.equalsIgnoreCase("navigateBrowser")){
						navigateBrowser(myD, vTestData);
						}
							if(vKW.equalsIgnoreCase("clickElement")){
							clickElement(myD, vXpath);
							}
								if (vKW.equalsIgnoreCase("verifyText")){
									System.out.println("Test is a" + verifyText(myD, vXpath, vTestData));
								}
								
						}
						
					}
				}
			}
	}
		
		
		
				
		// Capture o/p results
		
	
	

	
	//Read from Excel

	public String[][]  xlRead(String sPath, String iSheet) throws Exception{   // This line xlRead is the call to this function. We are passing the xl path in this
		//String[][] myExcel = null;
			//int xRow, xCol; commenting as I will declare it main code.
			String[][] xData;
		
		//file, wb,sheets
		File myxl = new File(sPath); // File is telling Java that it will use I/p o/p streams to interact with Java. The name of the file is myxl here.		
		FileInputStream myStream = new FileInputStream(myxl); //  FIle input stream reads i/p and o/p into the file  		
		HSSFWorkbook myWB = new HSSFWorkbook(myStream); // Creates workbook within this file.			
		HSSFSheet mySheet = myWB.getSheet(iSheet);	// getSheetAt(0) method - Referring to 1st sheet, 1 -2nd sheet and so on
		
		//Get row count
		xRow = mySheet.getLastRowNum()+1;  //gets number of rows in my sheet. SInce it starts with 0, adding +1 to it will get total number of rows which is the last row.
		xCol = mySheet.getRow(0).getLastCellNum(); // to get cells or number of columns, first go to row 0, then get the last cell number of that row.
		xData = new String[xRow][xCol];  // we are creating an Array of type string. This puts the number of rows and cols.		
				
		System.out.println("Rows are " + xRow);
		System.out.println("Cols are " + xCol);
		
		// Go to each cell
		for (int i = 0; i < xRow; i++) {                           
			HSSFRow row = mySheet.getRow(i); // select and point to a specific row in my sheet it points according to the iteration
			for (int j = 0; j < xCol; j++) {                               
				HSSFCell cell = row.getCell(j);        // on that row, get the value of each column of each row.                       
				String value = cellToString(cell);   //convert a specific cell type(int,string/double etc) to a simple string  - this I can get from the function HSSFcell                 
				xData[i][j] = value;     
				
			
				}   
			
		}
		return xData;
	}
	
	
// A new function to convert an object of type excel cell to a string value.
	public static String cellToString(HSSFCell cell) {
	// This function will convert an object of type excel cell to a string value
        int type = cell.getCellType();  //cell.getcell type returns an integer which
        Object result;
        switch (type) {
            case HSSFCell.CELL_TYPE_NUMERIC: //0
			HSSFCell cellSONum = null;
			result = cell.getNumericCellValue();
            	//result = String.valueOf((int)cell);
            	result = String.valueOf((int)cellSONum.getNumericCellValue());
                break;
            case HSSFCell.CELL_TYPE_STRING: //1
                result = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_FORMULA: //2
                throw new RuntimeException("We can't evaluate formulas in Java");
            case HSSFCell.CELL_TYPE_BLANK: //3
                result = "-";
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN: //4
                result = cell.getBooleanCellValue();
                break;
            case HSSFCell.CELL_TYPE_ERROR: //5
                throw new RuntimeException ("This cell has an error");
            default:
                throw new RuntimeException("We don't support this cell type: " + type);
        }
        return result.toString();
    }
	
	public void enterText(FirefoxDriver driver, String xPath, String Data) {
		// Purpose: Enter text into a edit field
		//i/p: driver, xPath, Data
		//o/p:  Nil
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.xpath(xPath)).clear();//some applications already have a default values in edit field, so this line is to clear the default path
		driver.findElement(By.xpath(xPath)).sendKeys(Data);
		
	}
	

	public void enterNum(FirefoxDriver driver, String xPath, CharSequence[] Data) {
		// Purpose: Enter text into a edit field
		//i/p: driver, xPath, Data
		//o/p:  Nil
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.xpath(xPath)).clear();//some applications already have a default values in edit field, so this line is to clear the default path
		driver.findElement(By.xpath(xPath)).sendKeys(Data);
		
	}
	
	
	public String readText(FirefoxDriver driver, String xPath) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: read text from any field.
		//i/p: driver, xPath
		//o/p:  Data
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		return driver.findElement(By.xpath(xPath)).getText(); //returns whatever comes here
	}
	
	public String verifyText(FirefoxDriver driver, String xPath, String vText) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: verifies text from any field.
		//i/p: driver, xPath
		//o/p:  Data
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		if (vText.equalsIgnoreCase(driver.findElement(By.xpath(xPath)).getText())){  //returns whatever comes here
			
			return "pass"; 
			
		} else {
			return "Fail";
		}
		
		
	}
	
	
	
	public void clickElement(FirefoxDriver driver, String xPath) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: Click any element on the AUT
		//i/p: driver, xPath
		//o/p:  NIl
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.xpath(xPath)).click(); // sometimes click may not work, so I write a code to send enter
		//driver.findElement(By.linkText(linkText)).click();
		//driver.findElement(By.xpath(xPath)).sendKeys("\n");
		
	}
		
	public void clickElementbylink(FirefoxDriver driver, String link) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: Click any element on the AUT
		//i/p: driver, xPath
		//o/p:  NIl
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.linkText(link)).click();
		driver.findElement(By.linkText(link)).sendKeys("\n");
		
	}
		
	
		public void selectList(FirefoxDriver driver, String xPath, String Data) { 
			// Purpose: Select from a drop down list 
			//i/p: driver, xPath, Data
			//o/p:  NIl
			//CreatedBy : Priya
			//When :8/05/14
			//EditedBy : Priya
			//When : 8/5/14
			
		Select myList = new Select(driver.findElement(By.xpath(xPath)));//creating memory in myList
		myList.selectByVisibleText(Data);
		myList = null;  //clears the memory in my list
			
		}
			
			
	
		public void checkBox(FirefoxDriver driver, String xPath) {  //this has to be checked.
			// Purpose: checks a checkbox
			//i/p: driver, xPath
			//o/p:  NIl
			//CreatedBy : Priya
			//When :8/05/14
			//EditedBy : Priya
			//When : 8/5/14
			
		//Is it checked?
			if(driver.findElement(By.xpath(xPath)).getAttribute("checked").equalsIgnoreCase("checked")){
				//then do nothing
		
			} else {
			//click
			driver.findElement(By.xpath(xPath)).click();
			}		
		}

	//uncheckBox
	// selectRadio
	
	public void navigateBrowser(FirefoxDriver driver, String Data){
		// Purpose: Navigates a browser to a URL
					//i/p: driver, xPath
					//o/p:  NIl
					//CreatedBy : Priya
					//When :8/05/14
					//EditedBy : Priya
					//When : 8/5/14
		
		driver.get(Data);
		//driver.navigate().to(Data);
		
	}

	
}




